 package com.devicemantra.coordinate.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;


import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.devicemantra.coordinate.dataobjects.CContact;


public class CExcelWriter 
{
	private OutputStream mOutputStream;
		
	public CExcelWriter(OutputStream pInputStream)
	{
		/* Set the inputstream for this reader */
		mOutputStream = pInputStream;  
	}
	
	public ArrayList<CContact> getContacts()
	{
		ArrayList<CContact> lListContacts = new ArrayList<CContact>();
		/* Start reading from the excel file and populate the contacts list */
		try
			{
				String lMessage = null;
				HSSFWorkbook lExcelWorkBook = new HSSFWorkbook();
				HSSFSheet lExcelSheet = lExcelWorkBook.getSheetAt(0);
				Iterator lRowIterator = lExcelSheet.rowIterator();
				CContact lContact =null;
				/* Let us skip the first row which has the labels - Name, Designation , Mobile ...... */
				lRowIterator.next();
				int lCounter = 1;
				while(lRowIterator.hasNext()) 
				{
					Row lContactRow = (Row)lRowIterator.next();
					try
					{
					lContact = getContact(lContactRow);
					} catch(Exception e)
					{
						System.out.println("****** Exception caught *******");
						continue;
					}
					lListContacts.add(lContact);
				}
	
			}
			catch(Exception e) 
			{ 
				System.out.println("Exception caught :");
				e.printStackTrace();
			}
			return lListContacts;
	}
	
	private boolean isNull(String pValue)
	{
		if(pValue == null )
		{
			System.out.println("Value was null");
			return true;
		}
		else
		{
			return false;
		}
			
	}
	
	private CContact getContact(Row pRowInfo)
	{
			Iterator lCellIterator = pRowInfo.cellIterator(); 
			String lStr = null;
			CContact lContactInfo = new CContact();
			/*lContactInfo.setDepartmentId("JUSTICE_1");
			lContactInfo.setDepartmentName("JUSTICE"); */
			
			/* Get the Name of the Contact */
			Cell cell = (Cell)lCellIterator.next();
			System.out.println("Processing Name " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setContactName(lStr);
			
			lStr = null;
			/* Get the Designation of the Contact */
			cell = (Cell)lCellIterator.next();
			System.out.println("Processing Designation " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setDesignation(lStr);
			
			lStr = null;
			/* Get the e-CoordEmail id of the Contact */
			cell = (Cell)lCellIterator.next();
			System.out.println("Processing Email Id " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setCoordEmailId(lStr);
			
			lStr = null;
			/* Get the e-CoordMobileId number of the Contact */
			cell = (Cell)lCellIterator.next();
			System.out.println("Processing Mobile number " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setCoordMobileId(lStr);
			
			lStr = null;
			/* Get the e-CoordTele number of the Contact */
			cell = (Cell)lCellIterator.next();
			System.out.println("Processing Office  Number " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setCoordTeleId(lStr);
			
			lStr = null;
			/* Get the e-CoordFax id of the Contact */
			cell = (Cell)lCellIterator.next();
			System.out.println("Processing Fax  Number " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setCoordFaxId(lStr);
			
			lStr = null;
			/* Get the e-Coord Frame id of the Contact */
			cell = (Cell)lCellIterator.next();
			System.out.println("Processing Frame Id " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setCoordFrameId(lStr);
			
			lStr = null;
			/* Get the e-CoordDesk id of the Contact */
			cell = (Cell)lCellIterator.next();
			System.out.println("Processing CoordDesk id  " +  " -  [" + cell.getRichStringCellValue().getString() + "]");
			lStr = cell.getRichStringCellValue().getString();
			isNull(lStr);
			lContactInfo.setCoordDeskId(lStr);
			return lContactInfo;
			
	}
	
}	
